<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/font-awesome-4.7.0/css/font-awesome.min.css"
          media="all">
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-tab-content" style="height: auto">
                <form class="layui-form" lay-filter="component-form-element" action="">

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">菜单名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="title" lay-verify="title" id="title"
                                           autocomplete="off" placeholder="请输入菜单名称" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-xs1">
                                <div class="layui-form-mid layui-word-aux"
                                     style="color: red !important;padding: 10px !important;">*
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">上级菜单</label>
                                <div class="layui-input-block">
                                    <div id="demo3" class="xm-select-demo"></div>
                                </div>
                            </div>
                            <div class="layui-col-xs1">
                                <div class="layui-form-mid layui-word-aux"
                                     style="color: red !important;padding: 10px !important;">*
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">菜单路径</label>
                                <div class="layui-input-block">
                                    <input type="text" name="href" lay-verify="href" id="href"
                                           value="/WZSNW/rest/getRoute?type"
                                           autocomplete="off" placeholder="请输入菜单路径" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">权限标识</label>
                                <div class="layui-input-block">
                                    <input type="text" name="perms" lay-verify="perms" id="perms"
                                           autocomplete="off" placeholder="请输入权限标识" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">类型</label>
                                <div class="layui-input-block">
                                    <select name="type" id="type" lay-verify="type">
                                        <option value="">请选择</option>
                                        <option value="0">系统</option>
                                        <option value="1">目录</option>
                                        <option value="2">菜单</option>
                                        <option value="3">按钮</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-xs1">
                                <div class="layui-form-mid layui-word-aux"
                                     style="color: red !important;padding: 10px !important;">*
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">菜单图标</label>
                                <div class="layui-input-block">
                                    <input type="text" id="icon" lay-filter="icon" class="hide" name="icon">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">排序</label>
                                <div class="layui-input-block">
                                    <input type="number" min="1" id="sort" name="sort" autocomplete="off"
                                           class="layui-input" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
                                </div>
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="delflag" id="delflag" lay-skin="switch"
                                           lay-text="正常|已禁用" checked>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item  layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0;padding: 0px;">
                                <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">立即提交</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'form', 'laydate', 'layer', 'iconPickerFa', 'xmSelect'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate,
            layer = layui.layer,
            iconPickerFa = layui.iconPickerFa,
            xmSelect = layui.xmSelect;

        $.ajax({
            type: "post",
            url: "<%=request.getContextPath()%>/rest/system/getMenuListXmSelect",
            dataType: "json",
            async: false,
            success: function (msg) {
                xmSelect.render({
                    el: '#demo3',
                    name: 'parentTid',
                    model: {label: {type: 'text'}},
                    style: {
                        width: '100%',
                    },
                    layVerify: 'required',
                    radio: true,
                    clickClose: true,
                    tree: {
                        show: true,
                        strict: false,
                        expandedKeys: [-1],
                    },
                    height: 'auto',
                    data() {
                        return msg
                    },
                    on: function (data) {
                        //arr:  当前多选已选中的数据
                        var arr = data.arr;
                        //change, 此次选择变化的数据,数组
                        var change = data.change;
                        //isAdd, 此次操作是新增还是删除
                        var isAdd = data.isAdd;
                        console.log(arr);//得到select原始DOM对象
                        // if (arr.length == 0) {
                        //     $("#departmenttid").val("");
                        // } else {
                        //     $("#departmenttid").val(arr[0].tid);
                        // }
                    },
                })
            }
        })

        iconPickerFa.render({
            // 选择器，推荐使用input
            elem: '#icon',
            // fa 图标接口
            url: "<%=request.getContextPath()%>/huazhi/lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false
            search: true,
            // 是否开启分页
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {
                console.log(data);
                $("#icon").val(data.icon);
            },
            // 渲染成功后的回调
            success: function (d) {
                // console.log(d);
            }
        });

        //自定义验证规则
        form.verify({
            title: function (value, item) {
                if (value == "") {
                    return '菜单名称必填';
                }
            },
            type: function (value, item) {
                if (value == "") {
                    return '类型必填';
                }
            },
        })

        form.on('submit(*)', function (data) {
            var field = data.field
            var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
            $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/addMenu",
                dataType: "json",
                async: false,
                data: field,
                success: function (msg) {
                    setTimeout(function () {
                        layer.close(index);
                        if (msg == true) {
                            layer.msg('添加成功');
                        } else {
                            layer.msg(msg.errorMsg);
                        }
                    }, 2000);
                    setTimeout(function () {
                        var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                        parent.layer.close(indexs);//关闭弹出层
                    }, 4000);

                }
            })
            return false;
        })


    });
</script>
</body>
</html>